<?php
namespace User\Controller;
use Think\Controller;
use Think\Auth;

class UserController extends Controller
{
    public $token;
    public $user;
    public $wxuser;
    public function __construct()
    {
        parent::__construct();
        $auth = new Auth();
        if( !session(C('USER_AUTH_KEY')) )
        {
            //$this->redirect("/Home/Index/index");
            $this->error('登录超时，请重新登录', '/Home/Index/index');
        }

        $name = MODULE_NAME . '/'. CONTROLLER_NAME. '/'.ACTION_NAME;
        if(!$auth->check($name, session(C('USER_AUTH_KEY'))))
        {
            header("Content-type: text/html; charset=utf-8");
            die('您没有权限访问该功能！' . session(C('USER_AUTH_KEY')));
        }

        $users = M('Users')->where(array('id' => session(C('USER_AUTH_KEY'))))->find();
        $this->user = $users;

        $this->assign('thisUser', $users);
        //
        $wecha = M('Wxuser')->where(array('id' => $this->user['wx_id']))->field('id,wxname,wechat_type,appid,appsecret,wxid,weixin,headerpic,token')->limit(1)->find();

        session('token', $wecha['token'], 3600);
        $this->assign('wxuser', $wecha);
        $this->wxuser = $wecha;

        //$this->assign('wxuser', $wecha);

        $this->token=session('token');
        $this->assign('token', $this->token);
        //
    }

    //权限验证函数--增加
    protected function all_insert($name = '', $back = '/index'){
        $name = $name?$name:MODULE_NAME;
        $db = D($name);
        if($db -> create() === false){
            $this -> error($db -> getError());
        }else{
            $id = $db -> add();
            if($id){
                $m_arr = array('Img', 'Text', 'Voiceresponse','Lottery', 'Host', 'Product', 'Selfform','Vote', 'Estate', 'Reservation');
                if(in_array($name, $m_arr)){
                    $this -> handleKeyword($id, $name, $_POST['keyword'], intval($_POST['precisions']));
                }
                $this -> success('操作成功', U(MODULE_NAME . $back));
            }else{
                $this -> error('操作失败', U(MODULE_NAME . $back));
            }
        }
    }
    protected function insert($name = '', $back = '/index'){
        $name = $name?$name:MODULE_NAME;
        $db = D($name);
        if($db -> create() === false){
            $this -> error($db -> getError());
        }else{
            $id = $db -> add();
            if($id == true){
                $this -> success('操作成功', U(MODULE_NAME . $back));
            }else{
                $this -> error('操作失败', U(MODULE_NAME . $back));
            }
        }
    }
    protected function save($name = '', $back = '/index'){
        $name = $name?$name:MODULE_NAME;
        $db = D($name);
        if($db -> create() === false){
            $this -> error($db -> getError());
        }else{
            $id = $db -> save();
            if($id == true){
                $this -> success('操作成功', U(MODULE_NAME . $back));
            }else{
                $this -> error('操作失败', U(MODULE_NAME . $back));
            }
        }
    }
    protected function all_save($name = '', $back = '/index'){
        $name = $name?$name:MODULE_NAME;
        $db = D($name);
        if($db -> create() === false){
            $this -> error($db -> getError());
        }else{
            $id = $db -> save();
            \Think\Log::record('123456');
            if($id){

                $this -> success('操作成功', U(MODULE_NAME . $back));
            }else{
                $this -> error('操作失败', U(MODULE_NAME . $back));
            }
        }
    }

    public function handleKeyword($id, $module, $keyword = '', $precisions = 0, $delete = 0){
        $db = M('Keyword');
        $token = session('token');
        $db -> where(array('pid' => $id, 'token' => $token, 'module' => $module)) -> delete();
        $keyword = trim(trim($keyword), ',');
        if (!$delete){
            $data['pid'] = $id;
            $data['module'] = $module;
            $data['token'] = $token;
            $flag1 = strpos($keyword, ',');
            $flag2 = strpos($keyword, ' ');
            if($flag1 === false && $flag2 === false){
                $pk = explode('|', $keyword);
                if(count($pk) == 2){
                    $data['precisions'] = $pk[1];
                    $data['keyword'] = $pk[0];
                }else{
                    $data['precisions'] = $precisions;
                    $data['keyword'] = $keyword;
                }
                $db -> add($data);
            }else{
                if($flag1 === false){
                    $keyword = explode(' ', $keyword);
                    foreach ($keyword as $k => $v){
                        $pk = explode('|', $v);
                        if(count($pk) == 2){
                            $data['precisions'] = $pk[1];
                            $data['keyword'] = $pk[0];
                        }else{
                            $data['precisions'] = $precisions;
                            $data['keyword'] = $v;
                        }
                        $db -> add($data);
                    }
                }else{
                    $keyword = explode(',', $keyword);
                    foreach ($keyword as $k => $v){
                        $pk = explode('|', $v);
                        if(count($pk) == 2){
                            $data['precisions'] = $pk[1];
                            $data['keyword'] = $pk[0];
                        }else{
                            $data['precisions'] = $precisions;
                            $data['keyword'] = $v;
                        }
                        $db -> add($data);
                    }
                }
            }
        }
    }
    public function my_error($msg = '', $code = -1, $type = 'json')
    {
        $res['code'] = $code;
        $res['msg'] = $msg;
        if ($type == 'json') {
            echo json_encode($res);
        } else {
            echo $res;
        }
        return;
    }

    public function my_success($msg = '', $code = 1, $type = 'json')
    {
        $res['code'] = $code;
        $res['msg'] = $msg;
        if ($type == 'json') {
            echo json_encode($res);
        } else {
            echo $res;
        }
        return;
    }
}
